﻿Imports WPFGlue.Framework

Public Class NameStickyViewModel
    Inherits StickyViewModel


    Private Shared _Count As Integer = 0
    Private _Index As Integer
    Public Sub New()
        MyBase.New()
        _Count += 1
        _Index = _Count
        Debug.Print("{0} {1} Constructed", Me.GetType.Name, _Index)
    End Sub
    Protected Overrides Sub Finalize()
        Debug.Print("{0} {1} Finalized", Me.GetType.Name, _Index)
        MyBase.Finalize()
    End Sub


    Private _LastNameFirst As Boolean = False
    Public Property LastNameFirst() As Boolean
        Get
            Return _LastNameFirst
        End Get
        Set(ByVal value As Boolean)
            _LastNameFirst = value
        End Set
    End Property

    Public Shared ReadOnly FullNameProperty As DependencyProperty = DependencyProperty.Register("FullName", GetType(String), GetType(NameStickyViewModel), New FrameworkPropertyMetadata(Nothing, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault))
    Public Property FullName() As String
        Get
            Return GetValue(FullNameProperty)
        End Get
        Set(ByVal value As String)
            SetValue(FullNameProperty, value)
        End Set
    End Property


    Public Shared ReadOnly LastNameProperty As DependencyProperty = DependencyProperty.Register("LastName", GetType(String), GetType(NameStickyViewModel), New FrameworkPropertyMetadata(Nothing, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault))
    Public Property LastName() As String
        Get
            Return GetValue(LastNameProperty)
        End Get
        Set(ByVal value As String)
            SetValue(LastNameProperty, value)
        End Set
    End Property


    Public Shared ReadOnly FirstNameProperty As DependencyProperty = DependencyProperty.Register("FirstName", GetType(String), GetType(NameStickyViewModel), New FrameworkPropertyMetadata(Nothing, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault))
    Public Property FirstName() As String
        Get
            Return GetValue(FirstNameProperty)
        End Get
        Set(ByVal value As String)
            SetValue(FirstNameProperty, value)
        End Set
    End Property


    Public Function DetermineLastName(ByVal fullname As String) As String
        Dim result As String = fullname
        If result Is Nothing Then
            result = String.Empty
        Else
            result = result.Trim
            If lastNameFirst Then
                Dim pos As Integer = result.IndexOf(","c)
                If pos >= 0 Then
                    result = result.Substring(0, pos).Trim
                Else
                    result = String.Empty
                End If
            Else
                Dim pos As Integer = result.LastIndexOf(" "c)
                If pos >= 0 Then
                    result = result.Substring(pos).Trim
                Else
                    result = String.Empty
                End If
            End If
        End If
        Return result

    End Function

    Public Function DetermineFirstName(ByVal fullname As String) As String
        Dim result As String = fullname
        If result Is Nothing Then
            result = String.Empty
        Else
            result = result.Trim
            If LastNameFirst Then
                Dim pos As Integer = result.IndexOf(","c)
                If pos >= 0 Then
                    result = fullname.Substring(pos + 1).Trim
                End If
            Else
                Dim pos As Integer = result.LastIndexOf(" "c)
                If pos >= 0 Then
                    result = fullname.Substring(0, pos).Trim
                End If
            End If
        End If
        Return result
    End Function

    Public Function DetermineFullName(ByVal firstname As String, ByVal lastname As String) As String
        If firstname Is Nothing Then
            firstname = String.Empty
        Else
            firstname = firstname.Trim
        End If
        If lastname Is Nothing Then
            lastname = String.Empty
        Else
            lastname = lastname.Trim
        End If
        Dim result As String = String.Empty
        If LastNameFirst Then
            If String.IsNullOrEmpty(firstname) Or String.IsNullOrEmpty(lastname) Then
                result = lastname & firstname
            Else
                result = lastname & ", " & firstname
            End If
        Else
            If String.IsNullOrEmpty(firstname) Or String.IsNullOrEmpty(lastname) Then
                result = firstname & lastname
            Else
                result = firstname & " " & lastname
            End If
        End If
        Return result
    End Function

    Protected Overrides Sub OnPropertyChanged(ByVal e As System.Windows.DependencyPropertyChangedEventArgs)
        MyBase.OnPropertyChanged(e)
        Static changeInitiator As DependencyProperty = Nothing

        If changeInitiator Is Nothing Then
            changeInitiator = e.Property

            Select Case e.Property.Name
                Case "FullName"
                    FirstName = DetermineFirstName(FullName)
                    LastName = DetermineLastName(FullName)
                Case "FirstName", "LastName"
                    FullName = DetermineFullName(FirstName, LastName)
            End Select

            changeInitiator = Nothing
        End If
    End Sub

End Class
